//域名, 为了方便切换域名
var domain = (function () {
    //if (window.location.href.indexOf("192.168.1.104") > -1) {
    //    return "http://192.168.1.104:58535/";
    //}
    //if (window.location.href.indexOf("localhost") > -1) {
    //    return "http://localhost:1066/";
    //}
    return "http://" + window.location.host + "/";
})();
/*----------- PC url ----------*/
// var baseUrl = domain + "cpi/";
var baseUrl = domain + "jiuyang/data/";
function PostWebApi(command, options, callback) {
    var _options = {
        donecallback: function () {

        },
        alwayscallback: function () {

        },
        always: function (alwayscallback) {
            this.alwayscallback = alwayscallback;
            return this;
        },
        done: function (donecallback) {
            this.donecallback = donecallback;
            return this;
        },
        data: {}
    };
    if (typeof options === "function") {
        callback = options;
        options = {};
    }
    _options.data = $.extend({},options,{});
    _options.donecallback = callback;
    $.ajax({
        url: baseUrl + command,
        method: "post",
        dataType: "json",
        contentType: "application/json",
        data: JSON.stringify(_options.data)
    }).done(function (data) {
        _options.donecallback.call(_options, data);
    }).always(function () {
        _options.alwayscallback.call(_options);
    });
    return _options;
}


/*----------- APP url ----------*/
var appBaseUrl = domain + "dazhi/app_data/";
function PostWebApi(command, options, callback) {
    var _options = {
        donecallback: function () {

        },
        alwayscallback: function () {

        },
        always: function (alwayscallback) {
            this.alwayscallback = alwayscallback;
            return this;
        },
        done: function (donecallback) {
            this.donecallback = donecallback;
            return this;
        },
        data: {}
    };
    if (typeof options === "function") {
        callback = options;
        options = {};
    }
    _options.data = $.extend({},options,{});
    _options.donecallback = callback;

    var token = window.localStorage.getItem('token');

    $.ajax({
        url: baseUrl + command,
        method: "post",
        dataType: "json",
        contentType: "application/json",
        headers: {'Content-Type': 'application/json;charset=utf8', 'token': token},
        data: JSON.stringify(_options.data)
    }).done(function (data) {
        if(data.code == 5){
            window.localStorage.removeItem("token");
            window.top.location.href = "/dazhi/app/login";
        } else {
            _options.donecallback.call(_options, data);
        }
    }).always(function () {
        _options.alwayscallback.call(_options);
    });
    return _options;
}

